//
//  LoginViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/5.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetViewController.h"
#import "taskUserManagerService.h"
#import "UserCenter.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passWordField;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initUI];
}

-(void)viewWillAppear:(BOOL)animated{
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = YES;
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
}

-(void)initUI
{
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction)]];
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.borderWidth = 1.0f;
    self.loginButton.layer.borderColor = [UIColor clearColor].CGColor;
    self.loginButton.layer.cornerRadius = 24.0f;
    
    [self.phoneField setValue:[UIColor colorWithWhite:1 alpha:0.8] forKeyPath:@"_placeholderLabel.textColor"];
    [self.passWordField setValue:[UIColor colorWithWhite:1 alpha:0.8] forKeyPath:@"_placeholderLabel.textColor"];
    
    NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"忘记密码"];
    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,[tncString length]}];
    [tncString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]  range:NSMakeRange(0,[tncString length])];
    [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor whiteColor] range:(NSRange){0,[tncString length]}];
    [self.forgetButton setAttributedTitle:tncString forState:UIControlStateNormal];
}

- (IBAction)loginAction:(id)sender {
    //1：手机号登录    0：编号登录
    NSString *mType = [Check isMobileNumber:self.phoneField.text] ?@"1" :@"0";
    if (![Check checkLenght:self.passWordField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    if (self.passWordField.text.length<6) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        return;
    }
    [DKProgressHUD showLoadingWithStatus:@"加载中..."];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.phoneField.text value:@"userNo"];
    [dict setField:self.passWordField.text value:@"password"];
    [dict setField:mType value:@"type"];
    [taskUserManagerService getLogin:dict success:^(dataLoginUser *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            if (response.data) {
                NSString *token = response.data.token;
                [[NetworkManager sharedManager].requestSerializer setValue:token forHTTPHeaderField:@"token"];
                [[NSUserDefaults standardUserDefaults] setObject:token forKey:SAVE_KEY_AUTH];
//                [UserCenter shareCenter].userNo = self.phoneField.text;
                [UserCenter shareCenter].userId = response.data.userId;
                [UserCenter shareCenter].authToken = token;
                [[UserCenter shareCenter] getProfiles:^(dataGetUserInfo *profile) {
                     [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                     [[NSNotificationCenter defaultCenter] postNotificationName:Notification_login_ok_notice object:nil userInfo:nil];
                }];
                return;
            }
            NSLog(@"登录成功，但未返回token");
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (IBAction)registerAction:(id)sender {
    [self.navigationController pushViewController:[RegisterViewController new] animated:YES];
}

- (IBAction)forgetAction:(id)sender {
    [self.navigationController pushViewController:[ForgetViewController new] animated:YES];
}

- (IBAction)pwdVisableAction:(id)sender {
    UIButton *button = sender;
    button.selected = !button.selected;
    self.passWordField.secureTextEntry = !button.selected;
}


-(void)tapSliderAction
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
